package com.ruoyi.swla2.util;


import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;
import org.locationtech.proj4j.*;

public class HelloProj4J {
    public static void main(String[] args) {
//        CRSFactory crsFactory = new CRSFactory();
//        //源坐标系统
//
//        //根据投影字符串参数获取坐标系统
//        String SourceCRS = "4524";
//        String SourceCRS_params = "+proj=tmerc +lat_0=0 +lon_0=108 +k=1 +x_0=36500000 +y_0=0 +ellps=GRS80 +units=m +no_defs";
//        CoordinateReferenceSystem source = crsFactory.createFromParameters(SourceCRS, SourceCRS_params);
//
//        //根据名称获取坐标系统
//        //CoordinateReferenceSystem source = crsFactory.createFromName("epsg:4524");
//
//        //目标坐标系统 根据投影字符串参数获取坐标系统
//        String TargetCRS = "2334";
//        String TargetCRS_params = "+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=20500000 +y_0=0 +ellps=IAU76 +units=m +no_defs";
//        CoordinateReferenceSystem target = crsFactory.createFromParameters(TargetCRS, TargetCRS_params);
//
//        //根据名称获取坐标系统
//        //CoordinateReferenceSystem  target =  crsFactory.createFromName("epsg:2334");
//
//        //定义转换类
//        CoordinateTransformFactory ctf = new CoordinateTransformFactory();
//        CoordinateTransform transform = ctf.createTransform(source, target);
//
//        //坐标系转换
//        ProjCoordinate projCoordinate = new ProjCoordinate(37360817.569479, 5127237.510467304);
////        ProjCoordinate projCoordinate = new ProjCoordinate(119.0632442, 45.7414338);
////        LINESTRING (20660567.6709131 5069420.014665496, 20660566.68679551 5069301.20789)
//        transform.transform(projCoordinate, projCoordinate);
//        System.out.println("转换后x:" + projCoordinate.x);
//        System.out.println("转换后y:" + projCoordinate.y);


        try {
           epsgTo4490(1);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static void epsgTo4490(int sourceEpsg) throws ParseException {
        String sourceEPSG = "EPSG:4524";
        String targetEPSG = "EPSG:2334";

        // Create MathTransform
        CRSFactory crsFactory = new CRSFactory();
        org.locationtech.proj4j.CoordinateReferenceSystem sourceCRS = crsFactory.createFromName(sourceEPSG);
        org.locationtech.proj4j.CoordinateReferenceSystem targetCRS = crsFactory.createFromName(targetEPSG);
        CoordinateTransformFactory transformFactory = new CoordinateTransformFactory();
        CoordinateTransform transform = transformFactory.createTransform(sourceCRS, targetCRS);


        // Convert the string representation of the geometry to a JTS Geometry object
        WKTReader reader = new WKTReader();
        Geometry geometry = reader.read("LINESTRING (37360817.569479 5127237.510467304, 37360830.13825466 5127118.315033647)");

        // Perform the coordinate transformation for each coordinate in the geometry
        for (int i = 0; i < geometry.getCoordinates().length; i++) {
            Coordinate srcCoord = geometry.getCoordinates()[i];
            ProjCoordinate targetCoord = new ProjCoordinate(srcCoord.getX(), srcCoord.getY());
            transform.transform(targetCoord, targetCoord); // 将源坐标转换为目标坐标，并保存在 targetCoord 中
            srcCoord.setX(targetCoord.x);
            srcCoord.setY(targetCoord.y);
        }

        // Convert the transformed geometry back to a string
        WKTWriter writer = new WKTWriter();
        String transformedShape = writer.write(geometry);
        System.out.println(transformedShape);

        // Update the original table with the transformed geometry using the primary key
    }

}
